<?php 
	header('Content-type:text/html;charset=utf-8');
	class ContentController extends CommonController{
		public $model;

		public function __init(){
			parent:: __init();
			$this->model=K('Shop_cart');
		}

		public function index(){
			//调取分类信息
			$cate = K('Shop_category')->where('pid=0')->all();
			$this->assign('cate',$cate);
			//调取商品表的信息
			$gid = Q('get.gid',0,'intval');
			$shopData = M()->join('__shop_goods__ g JOIN __shop_detail__ de ON g.gid=de.shop_goods_gid')->where("gid={$gid}")->find();
			if ($shopData) {
					$shopData['small'] = explode(',', $shopData['small']);
					$shopData['middle'] = explode(',', $shopData['middle']);
					$shopData['big'] = explode(',', $shopData['big']);
					$shopData['chajia'] = $shopData['markprice']-$shopData['shopprice'];	
				}	
				
			$this->assign('shopData',$shopData);
			//商品属性表查询
			//规格中颜色的调取
			$attrDataSe = M()->join('__shop_type_attr__ ta JOIN __Shop_goods_attr__ ga ON ta.taid=ga.shop_type_attr_taid')->group('taname')->order('taid asc')->field('taname,taid')->where("class=1 && shop_goods_gid={$gid}")->all();
			
			foreach ($attrDataSe as $k => $v) {
				$attrDataSe[$k]['value'] = K('Shop_goods_attr')->where("shop_type_attr_taid={$v['taid']} && shop_goods_gid={$gid}")->all();
			}
			$this->assign('attrDataSe',$attrDataSe);
			// p($attrDataSe);

			//品牌表中的查询
			$branData = M()->join('__shop_brand__ b JOIN __shop_goods__ g ON b.bid=g.shop_brand_bid')->where("gid={$gid}")->find();
			$this->assign('branData',$branData);
			//调取货品数量
			if (IS_POST) {
				$combine = $_POST['added'];
				$gid = $_POST['gid'];
				$pp = K('Shop_goods_list')->where("combine='{$combine}'")->find();
				// p($pp);	
				$this->ajax($pp['inventery']);
			}
			//购物车中的数据


			// $cartData = Cart::getAllData();
			// // p($cartData);
			// if ($cartData) {

			// 	$goods = $cartData['goods'];
				
			// }
			// $this->assign('goods',$goods);

			// $cartTotalPrice = Cart::getTotalPrice();

			// $this->assign('cartTotalPrice',$cartTotalPrice);

			// $cartTotalNum = Cart::getTotalNums();

			// $this->assign('cartTotalNum',$cartTotalNum); 

			$this->dis('content');
		}

		public function add(){

			$combine = $_POST['added'];
			$gid = $_POST['gid'];
			$pipei = K('Shop_goods_list')->where("combine='{$combine}'")->all();
			// p($pipei);die;
			if (empty($pipei)) {
				echo json_encode(0);die;
			}else{
				$gtid = explode(',', $_POST['added']);
				$attrData = array();
				$goodsData = k('Shop_goods')->where("gid={$gid}")->find();
				// p($goodsData);die;
				foreach ($gtid as $k => $v) {
					$attrData[]= K('Shop_goods_attr')->where("gtid={$v}")->find();
					$add = array();
					foreach ($attrData as $kk => $vv) {
						
						$add[] = $vv['added'];
						
					}
				}
				$add = array_sum($add);
				$NewPrice = $goodsData['shopprice'] + $add;
				// p($NewPrice);die;
				$this->ajax($NewPrice);
			}
			
		}

		public function cart(){
			$gid = Q('post.gid',0,'intval');
			$goodsData = K('Shop_goods')->where("gid={$gid}")->find();
			$gnumber = $goodsData['gnumber'];
			$name = Q('post.name');
			$num = Q('post.num');
			$price = Q('post.price');
			// $price = sprintf("%01.2f", $price);
			//去掉字符串结尾的3位
			$mark = substr(Q('post.mark'),0,strlen(Q('post.mark'))-3);
			// $mark = sprintf("%01.2f", $mark);
			$jiesheng = Q('post,jiesheng');
			$combine = Q('post.added');
			$added = explode(',', Q('post.added'));
			foreach ($added as $k => $v) {
				$attr[]= K('Shop_goods_attr')->where("gtid=$v")->getField('gtvalue');
			}
			
			$color=$attr[0];
			$size=$attr[1];
			
			//获得商品列表中的glid,以后获得库存用;
			$glid = K('Shop_goods_list')->where("combine='{$combine}' && shop_goods_gid={$gid}")->getField('glid');
			$inventery = K('Shop_goods_list')->where("combine='{$combine}' && shop_goods_gid={$gid}")->getField('inventery'); 
			// p($glid);
			//商品列表图
			$pic = $goodsData['pic'];
			$data = array(
				'id'   => $gid,
				'glid' =>$glid,
				'name' => $name,
				'num'  => $num,
				'inventery' => $inventery,
				'price'=> $price,
				'options'=>array(
					'gnumber'=>$gnumber,
					'color'  =>$color,
					'size'   =>$size,
					'pic'	=>$pic,
					'mark'  =>$mark,
					'jiesheng' =>$jiesheng,
					),
				
				);
			Cart::add($data);

			// p($_SESSION);
			// $this->ajax($v);
			$cartData = serialize(Cart::getAllData());
			//组数组，插入cart表
			$data1 = array(
				'jiesuan' => $cartData,
				'shop_user_uid' => $_SESSION['uid'],
				);
			$this->model->add($data1);
			$this->success('购物车插入成功');

		}

		public function gengxin(){

			$data = $this->model->getField('jiesuan',true);
			foreach ($data as $k => $v) {
				$tem = unserialize($v);
			}
			// p($tem);
			// p($_SESSION);
			$this->ajax($tem);
			
		}

	}



















?>